home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / x11 / font.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-05  |  4.0 KB  |  223 lines  |  [TEXT/????]

  1. /* X11 STDWIN -- font manipulation */
  2.  
  3. #include "x11.h"
  4.  
  5. TEXTATTR wattr;        /* Global text attributes */
  6.  
  7. /* Setting text drawing parameters. */
  8.  
  9. void
  10. wsetplain()
  11. {
  12.     wattr.style= PLAIN;
  13. }
  14.  
  15. void
  16. wsethilite()
  17. {
  18.     wattr.style |= HILITE;
  19. }
  20.  
  21. void
  22. wsetinverse()
  23. {
  24.     wattr.style |= INVERSE;
  25. }
  26.  
  27. void
  28. wsetitalic()
  29. {
  30.     wattr.style |= ITALIC;
  31. }
  32.  
  33. void
  34. wsetbold()
  35. {
  36.     wattr.style |= BOLD;
  37. }
  38.  
  39. void
  40. wsetbolditalic()
  41. {
  42.     wattr.style |= BOLD|ITALIC;
  43. }
  44.  
  45. void
  46. wsetunderline()
  47. {
  48.     wattr.style |= UNDERLINE;
  49. }
  50.  
  51. int
  52. wsetfont(fontname)
  53.     char *fontname;
  54. {
  55.     int i = fontnum(fontname);
  56.     if (i == 0)
  57.         return 0;
  58.     wattr.font= i;
  59.     _wfontswitch();
  60.     return 1;
  61. }
  62.  
  63. void
  64. wsetsize(pointsize)
  65.     int pointsize;
  66. {
  67.     /* Ignored for now; must be present for compatibility */
  68. }
  69.  
  70. void
  71. wgettextattr(attr)
  72.     TEXTATTR *attr;
  73. {
  74.     *attr= wattr;
  75. }
  76.  
  77. void
  78. wsettextattr(attr)
  79.     TEXTATTR *attr;
  80. {
  81.     wattr= *attr;
  82.     _wfontswitch();
  83. }
  84.  
  85. /* Font administration */
  86.  
  87. struct font {
  88.     char *name;        /* Font name, (lower case), NULL if unknown */
  89.     XFontStruct *info;    /* Text measuring info */
  90. };
  91.  
  92. static int nfonts;
  93. static struct font *fontlist;
  94.  
  95. /* Initialize the font stuff */
  96.  
  97. _winitfonts()
  98. {
  99.     struct font stdfont, menufont;
  100.     
  101.     /* Get the user-specified or server default font */
  102.     
  103.     stdfont.name= _wgetdefault("font", "Font");
  104.     if (stdfont.name != NULL) {
  105.         stdfont.info= XLoadQueryFont(_wd, stdfont.name);
  106.         if (stdfont.info != NULL) {
  107.             stdfont.name= strdup(stdfont.name);
  108.         }
  109.         else {
  110.             _wwarning(
  111.               "_winitfonts: can't load font %s; using server default",
  112.               stdfont.name);
  113.             stdfont.name= NULL;
  114.         }
  115.     }
  116.     if (stdfont.name == NULL) {
  117.         stdfont.info= XQueryFont(_wd, DefaultGCOfScreen(_ws)->gid);
  118.         if (stdfont.info == NULL)
  119.             _wfatal("_winitfonts: no server default font");
  120.         stdfont.info->fid= 0;
  121.     }
  122.     L_APPEND(nfonts, fontlist, struct font, stdfont);
  123.     _wmf= _wf= stdfont.info;
  124.     
  125.     /* Get the user-specified default menu font.
  126.        If no user-specified default, use the the standard font. */
  127.     
  128.     menufont.name= _wgetdefault("menuFont", "Font");
  129.     if (menufont.name != NULL) {
  130.         menufont.info= XLoadQueryFont(_wd, menufont.name);
  131.         if (menufont.info != NULL) {
  132.             menufont.name= strdup(menufont.name);
  133.             L_APPEND(nfonts, fontlist, struct font, menufont);
  134.             _wmf= menufont.info;
  135.         }
  136.         else {
  137.             _wwarning(
  138.               "_winitfonts: can't load font %s; using server default",
  139.               menufont.name);
  140.         }
  141.     }
  142. }
  143.  
  144. /* Start using the font from 'wattr' */
  145.  
  146. _wfontswitch()
  147. {
  148.     if (wattr.font < 0 || wattr.font >= nfonts)
  149.         _werror("_wfontswitch: wattr.font out of range");
  150.     else {
  151.         _wf= fontlist[wattr.font].info;
  152.         _wgcfontswitch();
  153.     }
  154. }
  155.  
  156. /* Return the font number for a given font name.
  157.    If not already in the font list, add it.
  158.    Return 0 (the number of the system font) if the font doesn't exist. */
  159.  
  160. static int
  161. fontnum(name)
  162.     char *name;
  163. {
  164.     struct font newfont;
  165.     char lcname[256];
  166.     int i;
  167.     
  168.     if (name == NULL || name[0] == EOS)
  169.         return 0; /* Use standard font */
  170.     
  171.     makelower(lcname, name);
  172.     for (i= 0; i < nfonts; ++i) {
  173.         if (fontlist[i].name != NULL &&
  174.             strcmp(fontlist[i].name, lcname) == 0)
  175.             return i;
  176.     }
  177.     newfont.info= XLoadQueryFont(_wd, lcname);
  178.     if (newfont.info == NULL) {
  179.         _wdebug(2, "fontnum: no font %s", lcname);
  180.         return 0; /* Not found; use system font */
  181.     }
  182.     _wdebug(1, "fontnum: new font %s", lcname);
  183.     newfont.name= strdup(lcname);
  184.     L_APPEND(nfonts, fontlist, struct font, newfont);
  185.     return i;
  186. }
  187.  
  188. /* Copy a string like strcpy, converting to lower case in the process */
  189.  
  190. static
  191. makelower(dest, src)
  192.     char *dest, *src;
  193. {
  194.     char c;
  195.     while ((c= *src++) != EOS) {
  196.         if (isupper(c))
  197.             c= tolower(c);
  198.         *dest++ = c;
  199.     }
  200.     *dest= EOS;
  201. }
  202.  
  203. /* List of font names returned by last wlistfontnames call */
  204.  
  205. static char **fontnames = NULL;
  206.  
  207. /* Return a list of all available font names matching a pattern */
  208.  
  209. char **
  210. wlistfontnames(pattern, p_count)
  211.     char *pattern;
  212.     int *p_count;
  213. {
  214.     if (pattern == NULL || pattern[0] == '\0')
  215.         pattern = "*";
  216.     if (fontnames != NULL) {
  217.         XFreeFontNames(fontnames);
  218.         fontnames = NULL;
  219.     }
  220.     fontnames = XListFonts(_wd, pattern, 5000, p_count);
  221.     return fontnames;
  222. }
  223.